<template>
	<div>
		<!--新增界面-->
		<el-dialog title="新增方案" :visible.sync="dialogVisible" v-loading.fullscreen.lock="loading" element-loading-text="处理中......" size="large" :close-on-click-modal="false" :close-on-press-escape="false" :before-close="off" :top="dialog_margin_top">
			  <el-form label-width="160px" >
			  <el-row :span="24">
			  	<el-col :span="8">
			  		<el-form-item label="方案类型" required>
	  					<el-select v-model="formData.plantype" filterable size="small"  @change="setDefault()">
							<el-option v-for="item in planTypeOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="险种代码" required>
						<el-select v-model="formData.risk" filterable value-key="riskcode" size="small"  @change="newPlanCode()">
							<el-option v-for="item in riskOptions" :key="item.riskcode" :label="item.riskcode + '-' + item.riskcname" :value="item"></el-option>
						</el-select>
					</el-form-item>
					<!-- 移除原因：点击保存时才生成条款代码 -->
					<!-- <el-form-item label="方案代码" required>
						<el-input v-model="formData.plancode" size="small"  disabled></el-input>
					</el-form-item> -->
					<el-form-item label="是否自动核保" required>
						<el-select v-model="formData.autoundwrt" filterable size="small" >
							<el-option v-for="item in autoUndwrtOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="方案有效性" required>
						<el-select v-model="formData.validstatus" size="small" >
							<el-option v-for="item in validOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="方案中文名称" required>
						<el-input v-model="formData.plancname" size="small" ></el-input>
					</el-form-item>
					<el-form-item label="方案英文名称" required>
						<el-input v-model="formData.planename" size="small" :maxlength="255"></el-input>
					</el-form-item>
			  	</el-col>
			  	<el-col :span="8">
					<el-form-item label="行程目的地">
						<el-input v-model="formData.traveldestination" size="small" :maxlength="500"></el-input>
					</el-form-item>
					<el-form-item label="职业分类代码">
						<el-input v-model="formData.occupationcode" size="small" :maxlength="10"></el-input>
					</el-form-item>
					<el-form-item label="条款是否允许删除" required>
						<el-select v-model="formData.delflag" :disabled="disabledGroup.delflag" size="small">
							<el-option v-for="item in delFlagOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="条款代码" required>
						<el-input v-model="formData.kindcodes" readonly size="small" @click.native="openRiskKindSelectDialog()"></el-input>
					</el-form-item>
          <el-form-item label="自动单证审核通过" required>
            <el-select v-model="formData.autodoc" filterable size="small" >
              <el-option v-for="item in autoDocOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
			  	</el-col>
			  	<el-col :span="8">
					<el-form-item label="责任代码">
						<el-input v-model="formData.itemcodes" readonly size="small" @click.native="openItemCodeSelectDialog()"></el-input>
					</el-form-item>
					<el-form-item label="预设保险期限方式">
						<el-select v-model="formData.periodtype" size="small">
							<el-option v-for="item in periodTypeOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="预设保险期限值">
						<el-input v-model="formData.period" size="small" :maxlength="5" @keyup.native="checkNumber(15, 'period')"></el-input>
					</el-form-item>
					<el-form-item label="方案权限适用机构" required>
						<el-input type="textarea" v-model="comLabel" size="small" readonly @dblclick.native="openPlan2CompanyDialog()"></el-input>
					</el-form-item>
					<el-form-item label="方案权限适用渠道">
						<el-input type="textarea" v-model="formData.applychannel" size="small" readonly @dblclick.native="openPlan2AgentDialog()"></el-input>
					</el-form-item>
			  	</el-col>
			  </el-row>
			</el-form>

			<el-row type="flex" :span="24" justify="space-around">
				<el-col :span="24">
					<el-table :data="formData.kinditems" border style="width: 100%;" :highlight-current-row="true">
						<el-table-column prop="kindcode" label="条款代码" width="130"></el-table-column>
						<el-table-column prop="kindcname" label="条款名称" show-overflow-tooltip width="300"></el-table-column>
						<el-table-column prop="itemcode" label="标的责任代码" width="130"></el-table-column>
						<el-table-column prop="itemcname" label="标的责任名称" show-overflow-tooltip min-width="400"></el-table-column>
						<el-table-column prop="oper" label="保额保费" width="150">
						      <template scope="props">
							<el-button type="default" size="small" @click="openAmountPremiumAddDialog(props.row)"><i class="fa fa-arrow-right"></i> 配置</el-button>
						      </template>
						</el-table-column>
					</el-table>
				</el-col>
			</el-row>

			  <span slot="footer" class="dialog-footer">
				   <el-button @click="off">取 消</el-button>
				   <el-button type="warning" @click="add">新 增</el-button>
			  </span>
		</el-dialog>
		<RiskKindSelect  v-if="showRiskKindSelectDialog"></RiskKindSelect>
		<ItemCodeSelect  v-if="showItemCodeSelectDialog"></ItemCodeSelect>
		<AmountPremiumAdd  v-if="showAmountPremiumAddDialog"></AmountPremiumAdd>
		<ComSelect  v-if="showComSelectDialog"></ComSelect>
		<AgentSelect  v-if="showPlan2AgentDialog"></AgentSelect>
	</div>
</template>

<script>

import PlanAPI from "../../../../js/production/plan/planAPI"
import Validations from "../../../../js/production/validations"
import RiskKindSelect from "./RiskKindSelect";
import ItemCodeSelect from "./ItemCodeSelect";
import AmountPremiumAdd from "./AmountPremiumAdd";
import ComSelect from "../../common/ComSelect";
import AgentSelect from "./AgentSelect";
export default {
  	components : {
  		RiskKindSelect,
	  	ItemCodeSelect,
	  	AmountPremiumAdd,
	  	ComSelect,
	  	AgentSelect,
	  },
	data() {
		return {
			dialogVisible: true,
			dialog_margin_top: '1%',
			formData: {
				plantype : '',
				risk: null,
				plancode : '',
				plancname : '',
				planename : '',
				validstatus : '1',
				kindcodes : '',
				kinds: [],
				itemcodes : '',
				items: [],
				kinditems: [],
				delflag :'0',
				autoundwrt : '',
				autodoc : null,
				traveldestination : '',
				periodtype: '3',
				period: '',
				applycomcode: '',
				applychannel: '',
				occupationcode: '',

			},
			comLabel : '',
			arrayComcodes : [],
			cacheKindItems: [],
			planTypeOptions: [
				{label : 'a-非定额', value : 'a'},
				{label : 'b-定额定险别', value : 'b'},
				{label : 'c-定险别定费率', value : 'c'},
				{label : 'd-定额定险别定费率定保费', value : 'd'},
			],
			periodTypeOptions: [
				{label : '天', value : '3'},
			],
			riskOptions : [],
			validOptions: [
				{label : '1-有效',value: '1'},
				{label : '0-无效',value: '0'}
			],
			delFlagOptions: [
				{label : '1-是',value: '1'},
				{label : '0-否',value: '0'}
			],
			autoUndwrtOptions: [
				{label: '默认',  value: ''},
				{label: 'Y-是',  value: 'Y'},
				{label: 'N-否 ',  value: 'N'},
			],
			autoDocOptions: [
        {label: '默认',  value: null},
				{label: 'Y-是',  value: 'Y'},
				{label: 'N-否 ',  value: 'N'},
			],
			showRiskKindSelectDialog: false,
			showItemCodeSelectDialog: false,
			showAmountPremiumAddDialog: false,
			showComSelectDialog: false,
			showPlan2AgentDialog: false,
			disabledGroup: {
				delflag : true,
			},
			currentKindItem: {},
			loading : false,
		}
	},
	mounted() {
		PlanAPI.initRiskSelect(this);
	},
	watch : {
		comLabel(newVal,oldVal) {
			this.formData.applycomcode = newVal;
			this.formData.applychannel = '';
		}
	},
	methods: {
		add() {
			PlanAPI.add(this);
		},
		off() {
			this.$parent.showAddDialog = false;
		},
		checkNumber(length, field) {
			Validations.checkNumber(length, this.formData, field);
		},
		newPlanCode() {
			this.formData.kindcodes = '';
			this.formData.kinds = [];
			this.formData.itemcodes = '';
			this.formData.items = [];
			this.formData.kinditems = [];
			// PlanAPI.newPlanCode(this); // 移除原因：点击保存时才生成方案代码
		},
		openRiskKindSelectDialog() {
			if (this.formData.risk === null) {
				this.$message({type: 'error',message: '请先选择险种代码！'});
				return ;
			}
			this.showRiskKindSelectDialog = true;
		},
		openItemCodeSelectDialog() {
			if(this.formData.kindcodes === '') {
				this.$message({type: 'error',message: '请先选择条款代码！'});
				return;
			}
			this.showItemCodeSelectDialog = true;
		},
		openAmountPremiumAddDialog(row) {
			this.currentKindItem = row;
			this.showAmountPremiumAddDialog = true;
		},
		openPlan2CompanyDialog() {
			this.showComSelectDialog = true;
		},
		openPlan2AgentDialog() {
			PlanAPI.openPlan2AgentDialog(this);
		},
		setDefault() {
			switch(this.formData.plantype) {
				case 'a': {
					this.disabledGroup.delflag = false;
					break;
				}
				case 'b': {
					this.formData.delflag = '0';
					this.disabledGroup.delflag = true;
					break;
				}
				case 'c': {
					this.formData.delflag = '0';
					this.disabledGroup.delflag = true;
					break;
				}
				case 'd': {
					this.formData.delflag = '0';
					this.disabledGroup.delflag = true;
					break;
				}
			}
		}
	}
  }
</script>
